// pages/u_yhq/u_yhq.js
const app = getApp()
var get_my_coupons;var page = 1;

// 请求数据
var loadMore = function (that) {
  that.setData({
    hidden: false,
    stopdowning: true
  });
  wx.request({
    url: get_my_coupons,
    data: {
      page: page,
      uid:that.data.aid,
      type:that.data.type
    },
    success: function (res) {
      console.log(res.data.list);
      var list = res.data.list;
      var goods = that.data.goods;
      if (list.length != 0) {
        for (var i = 0; i < list.length; i++) {
          list[i].info.amount = Number(list[i].info.amount).toFixed(0);
          list[i].info.full = Number(list[i].info.full).toFixed(0);
          goods.push(list[i]);
        }
      }
      that.setData({
        goods: goods,
        stopdowning: false
      });
      page++;
      if (goods.length == 0) {
        that.setData({
          hidden: true,
          stopdowning: true
        });
      } else {
        that.setData({
          hidden: false
        });
      }
    }
  });
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    head:[
      {name:'可使用',type:1},
      {name:'已使用',type:2},
      {name:'已过期',type:3}
    ],
    head_index:0,
    aid:'',
    type:'1',
    goods: [],
    stopdowning: false,
    hidden: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var aid = wx.getStorageSync('uid');
    get_my_coupons = app.data.ucenter_url + 'get_my_coupons';
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight)
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    }) 
    var goods = [];page = 1;
    that.setData({
      goods: goods,
      stopdowning: false,
      aid:aid
    })
    if (!that.data.stopdowning) {
      loadMore(that);
    }
  },
  headtap:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    var goods = [];page = 1;
    that.setData({
      goods: goods,
      stopdowning: false,
      type:type,
      head_index:index
    })
    if (!that.data.stopdowning) {
      loadMore(that);
    }
  },
  bindDownLoad: function () {
    var that = this;
    console.log(that.data.stopdowning);
    if (!that.data.stopdowning) {
      loadMore(that);
    } else {
      console.log('gg');
      return;
    }
  },
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})